
//
//  KNewfeatureViewController.m
//  DrugPin
//
//  Created by guohe on 2018/9/8.
//  Copyright © 2018年 outang. All rights reserved.
//

#import "KNewfeatureViewController.h"
#import "KLoginViewController.h"

#define NewfeatureCount 4
@interface KNewfeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIPageControl *pageControl;
@end

@implementation KNewfeatureViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupScrollView];
}

//创建UIScrollView并添加图片
- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = [UIScreen mainScreen].bounds;
    [self.view addSubview:scrollView];
    
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(NewfeatureCount*KScreenWidth, 0);
    scrollView.delegate = self;
    
    for (NSInteger i = 0; i < NewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.x = i * KScreenWidth;
        imageView.y = 0;
        imageView.width = KScreenWidth;
        imageView.height = KScreenHeight;
        NSString *name = [NSString stringWithFormat:@"0%ld登陆引导页",i+1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        //添加跳过按钮
        [self createSkipBt];
        if (i == NewfeatureCount - 1) {
            [self setupStartBtn:imageView];
        }
    }
    
    // 4.添加pageControl：分页，展示目前看的是第几页
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    [self.view addSubview:pageControl];

    pageControl.numberOfPages = NewfeatureCount;
    pageControl.backgroundColor = [UIColor redColor];
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = KColorOneAlpha(189, 189, 189);
    [pageControl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(KScreenWidth * 0.5);
        make.centerY.mas_equalTo(KScreenHeight - 50);
    }];
    
    self.pageControl = pageControl;
}

//左上角的灰色跳过按钮
-(void)createSkipBt
{
    UIButton *skipBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:skipBt];
    [skipBt setBackgroundImage:[UIImage imageNamed:@"jump"] forState:UIControlStateNormal];

    [skipBt mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).mas_offset(-40);
        make.top.equalTo(self.view).mas_offset(40);
        make.width.mas_equalTo(skipBt.currentBackgroundImage.size.width);
        make.height.mas_equalTo(skipBt.currentBackgroundImage.size.height);
    }];
    [skipBt addTarget:self action:@selector(BtnDidClicked) forControlEvents:UIControlEventTouchUpInside];
}

//手动拖拽结束时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入计算出页码
    self.pageControl.currentPage = (int)(page + 0.5);
    // 1.3四舍五入 1.3 + 0.5 = 1.8 强转为整数(int)1.8= 1
    // 1.5四舍五入 1.5 + 0.5 = 2.0 强转为整数(int)2.0= 2
    // 1.6四舍五入 1.6 + 0.5 = 2.1 强转为整数(int)2.1= 2
    // 0.7四舍五入 0.7 + 0.5 = 1.2 强转为整数(int)1.2= 1
}

//给最后一张图片添加 进入问医生按钮
- (void)setupStartBtn:(UIImageView *)imgView
{
    imgView.userInteractionEnabled = YES;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [imgView addSubview:btn];
    [btn setBackgroundImage:[UIImage imageNamed:@"experience"] forState:UIControlStateNormal];

    [btn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imgView);
        make.bottom.equalTo(imgView).mas_offset(-75);
        make.width.mas_equalTo(btn.currentBackgroundImage.size.width);
        make.height.mas_equalTo(btn.currentBackgroundImage.size.height);
    }];

    [btn addTarget:self action:@selector(BtnDidClicked) forControlEvents:UIControlEventTouchUpInside];
}

//立即体验按钮点击事件
-(void)BtnDidClicked
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[KLoginViewController alloc] init];
}

@end
